;#############################
;命令：
;nasm -f elf64 day2_0function_2function_with_multiple_parameters_0base.asm -o main.o && gcc main.o -o main -lc -no-pie && ./main
;输出：
;Value in arr[0] is 0.
;Value in arr[1] is 1.
;Value in arr[2] is 2.
;Value in arr[3] is 3.
;Value in arr[4] is 4.
;Value in arr[5] is 5.
;Value in arr[6] is 6.
;Value in arr[7] is 7.
;Value in arr[8] is 8.
;Value in arr[9] is 9.
;Value in arr[10] is 10.
;Value in arr[11] is 11.
;#############################
extern printf

section .data
	msg: db `Value in arr[%d] is %d.\n`, 0

section .bss
	arr: resd 12

section .text
	global main

main:
	push rbp
	mov rbp, rsp

	push 11
	push 10
	push 9
	push 8
	push 7
	push 6
	mov r9, 5
	mov r8, 4
	mov rcx, 3
	mov rdx, 2
	mov rsi, 1
	mov rdi, 0
	call this_is_my_printf

	add rsp, 48

	mov eax, 0
	leave
	ret

this_is_my_printf:
	push rbp
	mov rbp, rsp

;###########################################################
	;将所有的传入参数
	;逆序存放在rbp - 4 * 12内存上
	;重点是看push的值放在了哪里
	mov dword [arr + 4 * 0], edi
	mov dword [arr + 4 * 1], esi
	mov dword [arr + 4 * 2], edx
	mov dword [arr + 4 * 3], ecx
	mov dword [arr + 4 * 4], r8d
	mov dword [arr + 4 * 5], r9d
	;请注意push的内存地址不好确定是多少
	;首先已知地址与rbp有关
	;1、在没有进入函数之前打印push完最后一个数后的rsp
	;打印该内存
	;2、进入函数以后打印rbp
	;3、查看1、2内存的差值
	;4、通过rbp打印出改内存，看与1打印的内存是否一致
	mov rax, [rbp + 16 + 8 * 0]
	mov dword [arr + 4 * 6], eax
	mov rax, [rbp + 16 + 8 * 1]
	mov dword [arr + 4 * 7], eax
	mov rax, [rbp + 16 + 8 * 2]
	mov dword [arr + 4 * 8], eax
	mov rax, [rbp + 16 + 8 * 3]
	mov dword [arr + 4 * 9], eax
	mov rax, [rbp + 16 + 8 * 4]
	mov dword [arr + 4 * 10], eax
	mov rax, [rbp + 16 + 8 * 5]
	mov dword [arr + 4 * 11], eax
;###########################################################

;#################################################
	;打印
	mov ebx, 0
	jmp compare

execute:
	mov rdi, msg
	movsxd rsi, ebx
	movsxd rdx, dword [arr + 4 * ebx]
	call printf

	inc ebx

compare:
	cmp ebx, 12
	jl execute
;#################################################

	leave
	ret
